if (WIN32)
  set(INSTALL_LOCAL_DIR "${CMAKE_BINARY_DIR}/../../../../install/bin")
else()
  set(INSTALL_LOCAL_DIR "${CMAKE_BINARY_DIR}/bin")
endif()

set(data_dir ${CMAKE_CURRENT_SOURCE_DIR}/Data)
set(calib_dir ${CMAKE_CURRENT_SOURCE_DIR}/../CalibrationsFiles)
function(custom_add_executable)
  add_executable(${ARGV})
  if (WIN32)
    install(TARGETS ${ARGV0}
	    RUNTIME DESTINATION ${INSTALL_LOCAL_DIR}
	    LIBRARY DESTINATION ${VV_INSTALL_LIBRARY_DIR}
	    COMPONENT Runtime)
  endif(WIN32)
endfunction(custom_add_executable)
###############################################################################
custom_add_executable(TestVelodyneHDLPositionReader TestVelodyneHDLPositionReader.cxx)
target_link_libraries(TestVelodyneHDLPositionReader VelodynePlugin)

add_test(TestVelodyneHDLPositionReader
  ${INSTALL_LOCAL_DIR}/TestVelodyneHDLPositionReader
  "${data_dir}/HDL32-V2_R_into_Butterfield_into_Digital_Drive.pcap"
)

###############################################################################
custom_add_executable(TestVelodynePPSIdentification TestVelodynePPSIdentification.cxx)
target_link_libraries(TestVelodynePPSIdentification VelodynePlugin)

add_test(TestVelodynePPSIdentification
  ${INSTALL_LOCAL_DIR}/TestVelodynePPSIdentification
  "${data_dir}/HDL32-V2_R_into_Butterfield_into_Digital_Drive.pcap"
  "${data_dir}/VLP-16_Single/VLP-16_Single.pcap"
  "${data_dir}/VLP-16-with-GPRMC-with-PPS-timesync.pcap"
)
###############################################################################
custom_add_executable(TestVelodyneLidarStream TestVelodyneLidarStream.cxx)
target_link_libraries(TestVelodyneLidarStream VelodynePlugin)

custom_add_executable(TestVelodyneLidarReader TestVelodyneLidarReader.cxx)
target_link_libraries(TestVelodyneLidarReader VelodynePlugin)

set(sensors "HDL-64"
            "VLP-16"
            "VLP-32c")

# add test
foreach(sensor ${sensors})

    # LidarStream test
    add_test(TestVelodyneLidarStream_${sensor}_Single
      ${INSTALL_LOCAL_DIR}/TestVelodyneLidarStream
      ${data_dir}/${sensor}_Single/${sensor}_Single.pcap
      ${data_dir}/${sensor}_Single/GroundTruth/frames_list.txt
      ${calib_dir}/${sensor}.xml
    )
    add_test(TestVelodyneLidarStream_${sensor}_Dual
      ${INSTALL_LOCAL_DIR}/TestVelodyneLidarStream
      ${data_dir}/${sensor}_Dual/${sensor}_Dual.pcap
      ${data_dir}/${sensor}_Dual/GroundTruth/frames_list.txt
      ${calib_dir}/${sensor}.xml
    )

    # LidarReader test
    add_test(TestVelodyneLidarReader_${sensor}_Single
      ${INSTALL_LOCAL_DIR}/TestVelodyneLidarReader
      ${data_dir}/${sensor}_Single/${sensor}_Single.pcap
      ${data_dir}/${sensor}_Single/GroundTruth/frames_list.txt
      ${calib_dir}/${sensor}.xml
      ${data_dir}/${sensor}_Single/${sensor}_Single-reference-data.xml
    )
    add_test(TestVelodyneLidarReader_${sensor}_Dual
      ${INSTALL_LOCAL_DIR}/TestVelodyneLidarReader
      ${data_dir}/${sensor}_Dual/${sensor}_Dual.pcap
      ${data_dir}/${sensor}_Dual/GroundTruth/frames_list.txt
      ${calib_dir}/${sensor}.xml
      ${data_dir}/${sensor}_Dual/${sensor}_Dual-reference-data.xml
    )

endforeach(sensor)

# add special test for HDL-64 in autocalibration mode
# LidarStream test
add_test(TestVelodyneLidarStream_HDL-64_Single-autocalib
  ${INSTALL_LOCAL_DIR}/TestVelodyneLidarStream
  ${data_dir}/HDL-64_Single-autocalib/HDL-64_Single-autocalib.pcap
  ${data_dir}/HDL-64_Single-autocalib/GroundTruth/frames_list.txt
  ""
)
add_test(TestVelodyneLidarStream_HDL-64_Dual-autocalib
  ${INSTALL_LOCAL_DIR}/TestVelodyneLidarStream
  ${data_dir}/HDL-64_Dual-autocalib/HDL-64_Dual-autocalib.pcap
  ${data_dir}/HDL-64_Dual-autocalib/GroundTruth/frames_list.txt
  ""
)

set(apf_tests "VLS-128_APF")
foreach(apf_test ${apf_tests})
    # LidarStream test
    add_test(TestVelodyneLidarStream_${apf_test}
      ${INSTALL_LOCAL_DIR}/TestVelodyneLidarStream
      ${data_dir}/${apf_test}/${apf_test}.pcap
      ${data_dir}/${apf_test}/GroundTruth/frames_list.txt
      ${calib_dir}/VLS-128.xml
    )
    # LidarReader test
    add_test(TestVelodyneLidarReader_${apf_test}
      ${INSTALL_LOCAL_DIR}/TestVelodyneLidarReader
      ${data_dir}/${apf_test}/${apf_test}.pcap
      ${data_dir}/${apf_test}/GroundTruth/frames_list.txt
      ${calib_dir}/VLS-128.xml
      ${data_dir}/${apf_test}/${apf_test}-reference-data.xml
    )
endforeach(apf_test)

# LidarReader test
add_test(TestVelodyneLidarReader_HDL-64_Single-autocalib
  ${INSTALL_LOCAL_DIR}/TestVelodyneLidarReader
  ${data_dir}/HDL-64_Single-autocalib/HDL-64_Single-autocalib.pcap
  ${data_dir}/HDL-64_Single-autocalib/GroundTruth/frames_list.txt
  ""
  ${data_dir}/HDL-64_Single-autocalib/HDL-64_Single-autocalib-reference-data.xml
)
add_test(TestVelodyneLidarReader_HDL-64_Dual-autocalib
  ${INSTALL_LOCAL_DIR}/TestVelodyneLidarReader
  ${data_dir}/HDL-64_Dual-autocalib/HDL-64_Dual-autocalib.pcap
  ${data_dir}/HDL-64_Dual-autocalib/GroundTruth/frames_list.txt
  ""
  ${data_dir}/HDL-64_Dual-autocalib/HDL-64_Dual-autocalib-reference-data.xml
)

# cannot use above macro for HDL-128 because no Single/Dual pcaps
add_test(TestLidarStream_HDL-128
  ${INSTALL_LOCAL_DIR}/TestVelodyneLidarStream
  ${data_dir}/HDL-128/HDL-128_2019-10-25_25Kpackets.pcap
  ${data_dir}/HDL-128/GroundTruth/frames_list.txt
  ${calib_dir}/VLS-128.xml
)
add_test(TestLidarReader_HDL-128
  ${INSTALL_LOCAL_DIR}/TestVelodyneLidarReader
  ${data_dir}/HDL-128/HDL-128_2019-10-25_25Kpackets.pcap
  ${data_dir}/HDL-128/GroundTruth/frames_list.txt
  ${calib_dir}/VLS-128.xml
  ${data_dir}/HDL-128/HDL-128-reference-data.xml
)

###############################################################################
custom_add_executable(TestVelodyneStreamRecording TestVelodyneStreamRecording.cxx)
target_link_libraries(TestVelodyneStreamRecording VelodynePlugin)

add_test(TestVelodyneStreamRecording_VLP-32c_Dual
  ${INSTALL_LOCAL_DIR}/TestVelodyneStreamRecording
  ${data_dir}/VLP-32c_Dual/VLP-32c_Dual.pcap
  ${data_dir}/VLP-32c_Dual/GroundTruth/frames_list.txt
  ${calib_dir}/VLP-32c.xml
  ${data_dir}/VLP-32c_Dual/VLP-32c_Dual-reference-data.xml
)

custom_add_executable(TestVelodyneStreamForwarding TestVelodyneStreamForwarding.cxx)
target_link_libraries(TestVelodyneStreamForwarding VelodynePlugin)

add_test(TestVelodyneStreamForwarding_VLP-32c_Dual
  ${INSTALL_LOCAL_DIR}/TestVelodyneStreamForwarding
  ${data_dir}/VLP-32c_Dual/VLP-32c_Dual.pcap
  ${data_dir}/VLP-32c_Dual/GroundTruth/frames_list.txt
  ${calib_dir}/VLP-32c.xml
  ${data_dir}/VLP-32c_Dual/VLP-32c_Dual-reference-data.xml
)
###############################################################################


custom_add_executable(TestVelodyneTimeFramingReader TestVelodyneTimeFramingReader.cxx)
target_link_libraries(TestVelodyneTimeFramingReader VelodynePlugin)

add_test(TestVelodyneTimeFramingReader_HDL32
  ${INSTALL_LOCAL_DIR}/TestVelodyneTimeFramingReader
  ${data_dir}/HDL32-V2_R_into_Butterfield_into_Digital_Drive.pcap
  ${calib_dir}/HDL-32.xml
)


